<?php

namespace App\Http\Controllers\Member;

use App\User;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class UserController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth',[]);
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return view('member.user.index');
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    //显示头像修改等页面。动态改变
    public function create(Request $request,User $user)
    {
        $type= $request->toArray()['type'];
       return view('member.user.edit_'.$type);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\User  $user
     * @return \Illuminate\Http\Response
     */
    public function show(User $user)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\User  $user
     * @return \Illuminate\Http\Response
     */
    public function edit(User $user)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\User  $user
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, User $user)
    {

        $request->validate([
            'name'=>'sometimes|required',
            'email'=>'sometimes|required|email',
            'phone'=>'sometimes|required',
            'password'=>'sometimes|confirmed'
        ],[
                'name.required'=>'昵称不能为空',
                'email.required'=>'邮箱不能为空',
                'email.email'=>'邮箱格式不正确',
                'phone.required'=>'手机号码不能为空',
                'password.confirmed'=>'两次密码输入不正确',
        ]);
//     $user->update($request->all());
        $data=$request->all();
        if(isset($data['password'])){
            $data['password']=bcrypt($data['password']);
        }
        $user->update($data);
     return back()->with('success','操作成功');
    }
    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\User  $user
     * @return \Illuminate\Http\Response
     */
    public function destroy(User $user)
    {
        //
    }
}
